package com.yjian.service;

import com.yjian.common.CodeMap;
import com.yjian.common.SDataResult;
import com.yjian.common.StringUtils;
import com.yjian.dao.bean.Member;
import com.yjian.dao.bean.PayLog;
import com.yjian.dao.mapper.MemberMapper;
import com.yjian.dao.mapper.PayLogMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by xuxiaobao on 2017/9/30.
 */
@Service
public class MemberService {
    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private PayLogMapper payLogMapper;

    public Object getMemberListByUid(String uid) {
        Member member = new Member();
        member.initQuerySql(true,false,true,true,true,true,true);
        member.setUid(uid);
        List<Member> list = memberMapper.getMemberListByUid(member);
        if (list != null) {
            return SDataResult.success(list);
        }
        return new SDataResult(CodeMap.SUCCESS, new ArrayList<>());
    }

    @Transactional
    public Object saveMember(String uid, String phone, String username, Double balance, Integer number) {
        Member member = new Member();
        member.setUid(uid);
        member.setPhone(phone);
        int cnt = memberMapper.isExistMemberByPhone(member);
        if (cnt > 0) {
            return new SDataResult(CodeMap.FAIL, "会员已经存在");
        }

        member.setMid(StringUtils.randomKey());
        member.setUsername(username);
        member.setBalance(balance);
        member.setNumber(number);
        member.setCreateTime(new Date().getTime());

        int i = memberMapper.saveMember(member);
        if (i > 0) {
            return SDataResult.success("ok");
        }
        return SDataResult.fail();
    }

    public Object getMemberByPhone(String uid, String phone, Integer pageNo, Integer pageSize) {
        Pageable pageable = new PageRequest(pageNo-1,pageSize);
        Member member = new Member();
        member.initQuerySql(true,false,true,true,true,true,true);
        member.setUid(uid);
        member.setPhone(phone);
        int total = memberMapper.getMemberTotalByCondition(member);
        if (total <= (pageNo-1)*pageSize) {
            return SDataResult.success(new PageImpl<>(new ArrayList<>(),pageable,total));
        }
        member.setOffSet((pageNo-1)*pageSize);
        member.setPageSize(pageSize);
        List<Member> result = memberMapper.getMemberPageByCondition(member);
        return SDataResult.success(new PageImpl<>(result,pageable,total));
    }

    public Object getMemberByMid(String uid, String mid) {
        Member member = new Member();
        member.setUid(uid);
        member.setMid(mid);
        int i = memberMapper.isExistMemberAndUser(member);
        if (i <= 0) {
            return new SDataResult(CodeMap.FAIL, "会员不存在");
        }
        Member memberQuery = new Member();
        memberQuery.setMid(mid);
        memberQuery.initQuerySql(true,false,true,true,true,true,true);
        Member result = memberMapper.getMemberByMid(memberQuery);
        return SDataResult.success(result);
    }

    public Object getMemberPageByUid(String uid, String phone, Integer pageNo, Integer pageSize) {
        Member member = new Member();
        member.setUid(uid);
        member.setPhone(phone);
        int total = memberMapper.getMemberTotalByCondition(member);
        /*
        页码从0开始
         */
        Pageable pageable = new PageRequest(pageNo-1,pageSize);
        if ((pageNo-1)*pageSize >= total) {
            return SDataResult.success(new PageImpl<>(new ArrayList<>(),pageable,total));
        }
        member.setOffSet((pageNo-1)*pageSize);
        member.setPageSize(pageSize);
        member.initQuerySql(true,false,true,true,true,true,true);
        List<Member> result = memberMapper.getMemberPageByCondition(member);
        return SDataResult.success(new PageImpl<>(result, pageable, total));
    }

    @Transactional
    public Object consumeMemberBalance(String uid, String mid, Double balance) {
        Member member = new Member();
        member.setUid(uid);
        member.setMid(mid);
        int i = memberMapper.isExistMemberAndUser(member);
        if (i <= 0) {
            return new SDataResult(CodeMap.FAIL, "会员不存在");
        }
        member.initQuerySql(false,false,false,false,true,false,false);
        Member queryMember = memberMapper.getMemberByMid(member);
        if (queryMember.getBalance() - balance < 0.01) {
             return new SDataResult(CodeMap.FAIL, "账户余额不足");
        }
        //消费金额

        Member updateMember = new Member();
        updateMember.setMid(mid);
        updateMember.setBalance(queryMember.getBalance()-balance);

        int updateResult = memberMapper.updateMemberByMid(updateMember);
        if (updateResult <= 0) {
            return SDataResult.fail();
        }
        //添加充值记录

        PayLog payLog = new PayLog();
        payLog.setMid(mid);
        payLog.setMoney(balance);
        payLog.setNumber(0);
        payLog.setPayType(CodeMap.PAY_TYPE_CONSUME);
        payLog.setCreateTime(new Date().getTime());

        int payLogResult = payLogMapper.savePayLog(payLog);

        return SDataResult.success("ok");
    }

    @Transactional
    public Object consumeMemberNumber(String uid, String mid, Integer number) {
        Member member = new Member();
        member.setUid(uid);
        member.setMid(mid);
        int i = memberMapper.isExistMemberAndUser(member);
        if (i <= 0) {
            return new SDataResult(CodeMap.FAIL, "会员不存在");
        }
        member.initQuerySql(false,false,false,false,false,true,false);
        Member queryMember = memberMapper.getMemberByMid(member);
        if (queryMember.getNumber() - number < 0) {
            return new SDataResult(CodeMap.FAIL,"账户余次不足");
        }

        //消费次数

        Member updateMember = new Member();
        updateMember.setMid(mid);
        updateMember.setNumber(queryMember.getNumber() - number);
        int updateResult = memberMapper.updateMemberByMid(updateMember);
        if (updateResult <= 0) {
            return SDataResult.fail();
        }

        //添加消费记录

        PayLog payLog = new PayLog();
        payLog.setMid(mid);
        payLog.setMoney(0.0);
        payLog.setNumber(number);
        payLog.setPayType(CodeMap.PAY_TYPE_CONSUME);
        payLog.setCreateTime(new Date().getTime());

        int payLogResult = payLogMapper.savePayLog(payLog);

        return SDataResult.success("ok");
    }

    @Transactional
    public Object addMemberBalance(String uid, String mid, Double balance) {
        Member member = new Member();
        member.setUid(uid);
        member.setMid(mid);
        int i = memberMapper.isExistMemberAndUser(member);
        if (i <= 0) {
            return new SDataResult(CodeMap.FAIL, "会员不存在");
        }

        //充值
        member.initQuerySql(false,false,false,false,true,false,false);
        Member queryMember = memberMapper.getMemberByMid(member);
        Member updateMember = new Member();
        updateMember.setMid(mid);
        updateMember.setBalance(queryMember.getBalance() + balance);
        int updateResult = memberMapper.updateMemberByMid(updateMember);
        if (updateResult <= 0) {
            return SDataResult.fail();
        }

        //添加充值记录

        PayLog payLog = new PayLog();
        payLog.setMid(mid);
        payLog.setMoney(balance);
        payLog.setNumber(0);
        payLog.setPayType(CodeMap.PAY_TYPE_ADD);
        payLog.setCreateTime(new Date().getTime());
        payLogMapper.savePayLog(payLog);

        return SDataResult.success("ok");
    }

    @Transactional
    public Object addMemberNumber(String uid, String mid, Integer number) {
        Member member = new Member();
        member.setUid(uid);
        member.setMid(mid);
        int i = memberMapper.isExistMemberAndUser(member);
        if (i <= 0) {
            return new SDataResult(CodeMap.FAIL, "会员不存在");
        }

        //充次
        member.initQuerySql(false,false,false,false,false,true,false);
        Member queryMember = memberMapper.getMemberByMid(member);
        Member updateMember = new Member();
        updateMember.setMid(mid);
        updateMember.setNumber(queryMember.getNumber() + number);
        int updateResult = memberMapper.updateMemberByMid(updateMember);
        if (updateResult <= 0) {
            return SDataResult.fail();
        }

        //添加充次记录

        PayLog payLog = new PayLog();
        payLog.setMid(mid);
        payLog.setMoney(0.0);
        payLog.setNumber(number);
        payLog.setPayType(CodeMap.PAY_TYPE_ADD);
        payLog.setCreateTime(new Date().getTime());
        payLogMapper.savePayLog(payLog);

        return SDataResult.success("ok");
    }
}
